package packageHandler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import protocol.DataPackage;
import protocol.JsonPackage;
import util.DBUtil;
import frame.ChatFrame;

public class PackageHandlerForClient {
	public static void receiveLIST(DataPackage pack, List<String> userList, ChatFrame chatRoom, String loginName){
		userList.clear();
		System.out.println("server send userList to all people...");
		System.out.println(pack.getMessageData());
		String[] buf = pack.getMessageData().split("\\\\");
		chatRoom.removeAllModel();
		for(String i:buf){
			userList.add(i);
			if(!i.equals(loginName)){
				chatRoom.addModel(i);
			}
		}  
	}
	
	public static void receiveTEXT(DataPackage pack, List<String> userList, ChatFrame chatRoom){
		String Message = pack.getMessageData();
		long nowTime = pack.getTime();
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String retStrFormatNowDate = sdFormatter.format(nowTime);
		//显示在面板上
		chatRoom.setChatText(pack.getFromName() + ": " + pack.getMessageData() + " " + retStrFormatNowDate + "\n");
		
		//显示跟谁聊天
		chatRoom.setTitle("talk to " + pack.getFromName() + "...");
	}
	
	public static void receiveSHAKE(DataPackage pack, List<String> userList, ChatFrame chatRoom){
		long nowTime = pack.getTime();
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String retStrFormatNowDate = sdFormatter.format(nowTime);
		chatRoom.setChatText(pack.getMessageData() + " " + retStrFormatNowDate + "\n");
		chatRoom.shake();
		chatRoom.setTitle("talk to " + pack.getFromName() + "...");
	}
	
	public static void receiveLOGIN(DataPackage pack, List<String> userList, ChatFrame chatRoom){
		long nowTime = pack.getTime();
		SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		String retStrFormatNowDate = sdFormatter.format(nowTime);
		//显示在面板上
		chatRoom.setChatText(pack.getMessageData() + " " + retStrFormatNowDate + "\n");
	}
	
	public static boolean receiveLOGOUT(DataPackage pack, List<String> userList, 
			ChatFrame chatRoom, String loginName){
		if(pack.getToName().equals("\\")){
			DBUtil.logout(loginName);
			chatRoom.dispose();
			return false;
		}
		//自己要退出的，就返回false结束循环
		if(pack.getToName().equals(loginName)){
			DBUtil.logout(loginName);
			chatRoom.dispose();
			return false;
		}
		else{
			long nowTime = pack.getTime();
			SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String retStrFormatNowDate = sdFormatter.format(nowTime);
			System.out.println(retStrFormatNowDate + "date number////");
			chatRoom.setChatText(pack.getMessageData() + " " + retStrFormatNowDate + "\n");
		}
		return true;
	}
	
	public static void login(BufferedReader reader, BufferedWriter writer, String loginName){
		try {
			//发送登录信息
			DataPackage pack = new DataPackage(loginName, "\\", DataPackage.MessageType.LOGIN, "hello server," + loginName + " want to login in...", 0);	
			writer.write(new JsonPackage(pack).getJsonString() + "\n");
			writer.flush();
			System.out.println("login in client");
		} catch (Exception e) {
			System.out.println(loginName + " is log in....");
			e.printStackTrace();
		}
	}
	
	//发送给所有用户
	public static void logout(BufferedReader reader, BufferedWriter writer, String loginName){
		DataPackage pack = new DataPackage(loginName, "\\", DataPackage.MessageType.LOGOUT, (loginName + " is logout...\n"), 0);
		
		try {
//			writer.writeObject(pack);
			writer.write(new JsonPackage(pack).getJsonString() + "\n");
			writer.flush();
		} catch (IOException e) {
			System.out.println(loginName + " log out with a exception...");
			e.printStackTrace();
		}
	}
}
